package startup;


import java.util.ArrayList;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public class Test {

	protected Shell shell;
	private Table table;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Test window = new Test();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return null;
		}
	}

	class TableContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			if (parent instanceof List) {
				// return ((List) parent).toArray();
			}
			return new String[] { "One", "Two", "Three", "four" };
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
			}
		});
		shell.setSize(568, 420);
		shell.setText("SWT Application");

		ArrayList<String> arrayList;
		shell.setLayout(new FormLayout());
		
		

		//TableViewer tableViewer = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
		//table = tableViewer.getTable();
		table = new Table(shell,SWT.NONE);
		
		//table.setHeaderVisible(true);
		//table.setLinesVisible(true);
		//final GridData gd = new GridData(GridData.FILL_BOTH);
		TableLayout tableLayout = new TableLayout();
		table.setLayout(tableLayout);
		//table.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
		table.addListener(SWT.MeasureItem, new Listener() {
			// TODO 修改行高度
			public void handleEvent(Event event) {
				event.width = table.getGridLineWidth();
				// 设置宽度
				// event.height = (int) Math.floor(event.gc
				// .getFontMetrics().getHeight() * 1.5);
				event.height = 100;
				//event.gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
				// 设置高度为字体高度的1.5倍
			}
		});
		
		
		
		for (int i = 0; i < 5; i++) {
			new TableItem(table, SWT.NONE).setText("item " + i);
		}
		//table.setRedraw(true);
		
		
		final TableItem[] items = table.getItems();
		TableEditor editor = null;
		
		for (int i = 0; i < items.length; i++) {
			editor = new TableEditor(table);
			//editor.minimumWidth = 100;
			
			//editor.grabVertical =false;
			//editor.setEditor(button, items[i], 0);
			//items[i].
			//editor.
			
			
			final Composite cmp = new Composite(table,SWT.NONE);
			
			//cmp.pack();
			//System.out.println(cmp.getBorderWidth());//0
			//System.out.println(table.getGridLineWidth());
			
			Region r = cmp.getRegion();
			Point p = cmp.getSize();
			Rectangle rect = cmp.getClientArea();
			int width = rect.width;//table.getGridLineWidth();
			int height = rect.height;
			System.out.println("width = "+width+" height= "+height);
			final Button    b1  = new Button(cmp,SWT.NONE);
			b1.setText("button1");
			b1.setBounds(5,5,  30, 20);
			b1.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					b1.getParent().setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
					//table.redraw();
				}
			});
			b1.pack();
			
			
			final Button    b2  = new Button(cmp,SWT.NONE);
			b2.setBounds(100, 35, 30, 20);
			b2.setText("button2");
			b2.pack();
			
			final Button    b3  = new Button(cmp,SWT.NONE);
			b3.setBounds(100, 65, 30, 20);
			b3.setText("button3");
			b3.pack();
			
			cmp.addMouseTrackListener(new MouseTrackAdapter() {
				public void mouseEnter(MouseEvent e) {
					//composite.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
					cmp.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_CYAN));
					b1.setVisible(true);
					b2.setVisible(true);
					b3.setVisible(true);
				}
				@Override
				public void mouseExit(MouseEvent e) {
					cmp.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
					b1.setVisible(false);
					b2.setVisible(false);
					b3.setVisible(false);
				}
				@Override
				public void mouseHover(MouseEvent e) {
					
					
				}
			});
			editor.horizontalAlignment = SWT.LEFT;
			editor.grabHorizontal=true;
			editor.setEditor(cmp, items[i], 0);
		}
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				int total = table.getItemCount();
				//table.setRedraw(false);
				for (int i= 0; i < total; i++) {
					TableItem item = table.getItem(i);
					if (item.getChecked()) {
						System.out.println(i + "selected");
						item.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
						item.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
						
						//items[i].setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
//						shell.getDisplay().asyncExec(new Runnable(){
//							public void run(){
//								items[1].setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
//							}
//						});
					}else{
						//item.setBackground(null);
						//item.setForeground(null);
					}
					table.redraw();
					//table.setRedraw(true);
				}
			}
		});
		
		
		
		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				items[2].setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
				//table.redraw();
			}
		});
		FormData fd_button = new FormData();
		fd_button.left = new FormAttachment(0, 10);
		button.setLayoutData(fd_button);
		button.setText("New Button");
		
		final Composite composite = new Composite(shell, SWT.NONE);
		fd_button.top = new FormAttachment(composite, 54);
		final Button    b1  = new Button(composite,SWT.NONE);
		b1.setText("button1");
		b1.setBounds(5,5,  30, 20);
		b1.setVisible(false);
		b1.pack();
		
		
		final Button    b2  = new Button(composite,SWT.NONE);
		b2.setBounds(5, 35, 30, 20);
		b2.setText("button2");
		b2.setVisible(false);
		b2.pack();
		
		final Button    b3  = new Button(composite,SWT.NONE);
		b3.setBounds(5, 65, 30, 20);
		b3.setText("button3");
		b3.setVisible(false);
		b3.pack();
		
		
		composite.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(MouseEvent e) {
				//composite.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				composite.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
				b1.setVisible(false);
				b2.setVisible(false);
				b3.setVisible(false);
			}
			@Override
			public void mouseHover(MouseEvent e) {
				composite.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_CYAN));
				b1.setVisible(true);
				b2.setVisible(true);
				b3.setVisible(true);
				
			}
		});
		composite.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
			}
		});
		composite.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				composite.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
			}
			@Override
			public void focusLost(FocusEvent e) {
				composite.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
			}
		});
		composite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				composite.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLUE));
			}
		});
		FormData fd_composite = new FormData();
		fd_composite.bottom = new FormAttachment(0, 233);
		fd_composite.right = new FormAttachment(0, 116);
		fd_composite.top = new FormAttachment(0, 85);
		fd_composite.left = new FormAttachment(0, 10);
		composite.setLayoutData(fd_composite);
		// tableColumn.set
		// ColumnHeightData chd = new ColumnHeightData(30);
		
		FormData fd_table = new FormData();
		fd_table.right = new FormAttachment(0, 516);
		fd_table.top = new FormAttachment(0, 26);
		fd_table.bottom = new FormAttachment(0, 323);
		fd_table.left = new FormAttachment(0, 296);
		table.setBackground(new Color(shell.getDisplay(),100,100,90));
		System.out.println(table.getBackground());
		table.setLayoutData(fd_table);
		
		
		
		
		

	}
}
